/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui.undo.impl;

import java.awt.Color;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import org.apache.log4j.Logger;

import who.tmap.toolkit.MappingToolKit;
import who.tmap.toolkit.shape.MapShape;
import who.tmap.ui.undo.PolygonShapeUndoInfo;
import who.tmap.ui.undo.UndoAction;
import who.tmap.ui.undo.UndoActions;
import who.tmap.ui.undo.UndoManager;

public class UndoManagerImpl implements UndoManager {

    private static Logger logger = Logger.getLogger(UndoManagerImpl.class); 
	
	private Stack<UndoAction> undoActions;
	private Stack<UndoAction> redoActions;
	
	private MappingToolKit mtk;
	
	private static UndoManager instance =null;
	
	private UndoManagerImpl() {
		undoActions =new Stack<UndoAction>();
		redoActions =new Stack<UndoAction>();
	}
	
	public synchronized static final UndoManager getInstance() {
		if ( instance == null ) {
			instance =new UndoManagerImpl();
		}
		
		return instance;
	}	
	
	public void setMtk(MappingToolKit mtk) {
		this.mtk = mtk;
	}

	public UndoAction pop() {
		UndoAction action =null;
		if ( !undoActions.empty() ) {
			action =undoActions.pop();
			redoActions.push( action );
		}
		
		return action;
	}
	
	public UndoAction popRedo() {
		UndoAction action =null;
		if ( !redoActions.empty() ) {
			action =redoActions.pop();
			undoActions.push( action );
		}
		
		return action;
	}

	public void push(UndoAction action) {
		undoActions.push( action );
	}
	
	public void push( int action, Object obj ) {
		undoActions.push( new UndoActionImpl( action, obj ) );
	}
	
	public void push( int action, Object obj, Color color ) {
		undoActions.push( new UndoActionImpl( action, obj, color ) );
	}	
	
	public void push( int action, Object obj, Point2D anchorPt, Point2D dragPt ) {
		undoActions.push( new UndoActionImpl( action, obj, anchorPt, dragPt ) );
	}
	
	public void push( int action, Object obj, Point2D anchorPt, Point2D dragPt, int handleIdx ) {
		undoActions.push( new UndoActionImpl( action, obj, anchorPt, dragPt, handleIdx ) );
	}	
	
	public boolean empty() {
		return undoActions.empty();
	}
	
	public void clear() {
		undoActions.clear();
	}
	
	@SuppressWarnings("unchecked")
	public UndoAction undo() throws Exception {
		UndoAction undoAction =pop();
		MapShape shape =null;
		List<MapShape> shapes =null;
		
		if ( undoAction != null ) {
			switch( undoAction.getAction() ) {
				/*case UndoActions.CDPOINT_CIRCLE:
				case UndoActions.CDPOINT_CROSS:
				case UndoActions.CDPOINT_SQUARE:
				case UndoActions.CDPOINT_TRIANGLE:*/
				case UndoActions.CROSS:
				case UndoActions.ELLIPSE:
				case UndoActions.IMAGE:
				case UndoActions.LABEL:
				case UndoActions.LABELS_LIST:
				case UndoActions.LINE:
				case UndoActions.POLYGON:	
				case UndoActions.RECTANGLE:
				case UndoActions.TEXT:
				case UndoActions.TRIANGLE:
					shape =(MapShape)undoAction.getObject();

					mtk.removeShape( shape );
					break;
					
				case UndoActions.DELETE:
					shapes =(List<MapShape>)undoAction.getObject();

					mtk.addShapes( shapes );
					mtk.selectShapes( shapes );

					break;
					
				case UndoActions.MOVE:
					shapes =(List<MapShape>)undoAction.getObject();
					mtk.selectShapes( shapes );
					
					mtk.setAnchor( undoAction.getDragPt() );
					mtk.moveSelectedShapes( undoAction.getAnchorPt() );
					
					break;
					
				case UndoActions.MERGE_POLYGON:
					shapes =(List<MapShape>)undoAction.getObject();

					// remove the merged shape
					mtk.removeShape( shapes.get(2) );
					
					// add & select the original shapes
					List<MapShape> mapShapes =new ArrayList<MapShape>();
					mapShapes.add( shapes.get( 0 ) );				
					mapShapes.add( shapes.get( 1 ) );
					mtk.addShapes( mapShapes );
					mtk.selectShapes( mapShapes );

					break;
					
				case UndoActions.SPLIT_POLYGON:
					mapShapes =(List<MapShape>)undoAction.getObject();
					mtk.removeShape( mapShapes.get( 0 ) );
					mtk.removeShape( mapShapes.get( 1 ) );
					
					List<MapShape> theShapes =new ArrayList<MapShape>();
					theShapes.add( mapShapes.get( 2 ) );
					mtk.addShapes( theShapes );
					mtk.selectShapes( theShapes );
					
					break;					
					
				case UndoActions.INSERT_POLYGON_POINT:
				case UndoActions.DELETE_POLYGON_POINT:
				case UndoActions.MOVE_POLYGON_POINT:
					List<PolygonShapeUndoInfo> undoInfos =(List<PolygonShapeUndoInfo>)undoAction.getObject();
					for( PolygonShapeUndoInfo undoInfo : undoInfos ) {
						PolygonShapeUndoInfo undoInfoSav =new PolygonShapeUndoInfoImpl();
						undoInfoSav.cloneInfoFromPolygonShape( undoInfo.getPolygonShape() );

						undoInfo.getPolygonShape().setGeneralPath( undoInfo.getGeneralPath() );
						undoInfo.getPolygonShape().setDisputedBorders( undoInfo.getDisputedBorders() );
						
						undoInfo.setGeneralPath( undoInfoSav.getGeneralPath() );
						undoInfo.setDisputedBorders( undoInfoSav.getDisputedBorders() );
					}
				    
				    break;	    
				    
				case UndoActions.DISPUTED_BORDER:
					mtk.addDisputedBorder( (Point2D)undoAction.getObject() );
					
					break;
					
				case UndoActions.FILL:
					shape =(MapShape)undoAction.getObject();
					Color color =shape.getFillColor();
					shape.setFillColor( undoAction.getColor() );
					undoAction.setColor( color );
					
					break;

					
				case UndoActions.ADD_CD_POINT:
					List row =(List)undoAction.getObject();
					mtk.getThematicMap().getData().getRows().remove( row );
					
					break;
					
				case UndoActions.MOVE_POINT:
					shape =(MapShape)undoAction.getObject();
					shape.updateShape( undoAction.getHandleIdx(), undoAction.getAnchorPt() );
					
					break;

			}

		}
		
		return undoAction;
		
	}
	
	@SuppressWarnings("unchecked")
	public UndoAction redo() throws Exception {
		MapShape shape =null;
		List<MapShape> shapes =null;
		
		UndoAction redoAction =popRedo();
		
		if ( redoAction != null ) {
			
			switch( redoAction.getAction() ) {
			
				/*case UndoActions.CDPOINT_CIRCLE:
				case UndoActions.CDPOINT_CROSS:
				case UndoActions.CDPOINT_SQUARE:
				case UndoActions.CDPOINT_TRIANGLE:*/
				case UndoActions.CROSS:
				case UndoActions.ELLIPSE:
				case UndoActions.IMAGE:
				case UndoActions.LABEL:
				case UndoActions.LABELS_LIST:
				case UndoActions.LINE:
				case UndoActions.POLYGON:	
				case UndoActions.RECTANGLE:
				case UndoActions.TEXT:
				case UndoActions.TRIANGLE:
					shape =(MapShape)redoAction.getObject();
					shapes =new ArrayList<MapShape>();
					shapes.add( shape );
					mtk.addShapes( shapes );
					mtk.selectShape( shape );
					break;
					
				case UndoActions.DELETE:
					shapes =(List<MapShape>)redoAction.getObject();
					for( MapShape mapShape : shapes ) {
						mtk.removeShape( mapShape );
					}
					break;
					
				case UndoActions.MOVE:
					shapes =(List<MapShape>)redoAction.getObject();

					mtk.selectShapes( shapes );
					
					mtk.setAnchor( redoAction.getAnchorPt() );
					mtk.moveSelectedShapes( redoAction.getDragPt() );
					
					break;
					
				case UndoActions.MERGE_POLYGON:
					shapes =(List<MapShape>)redoAction.getObject();

					// remove the merged shapes
					mtk.removeShape( shapes.get( 0 ) );
					mtk.removeShape( shapes.get( 1 ) );	
					
					// add the merged shape
					List<MapShape> mapShapes =new ArrayList<MapShape>();
					mapShapes.add( shapes.get( 2 ) );				
					mtk.addShapes( mapShapes );
					mtk.selectShapes( mapShapes );

					break;		
					
				case UndoActions.SPLIT_POLYGON:
					mapShapes =(List<MapShape>)redoAction.getObject();
					mtk.removeShape( mapShapes.get( 2 ) );
					
					List<MapShape> theShapes =new ArrayList<MapShape>();
					theShapes.add( mapShapes.get( 0 ) );
					theShapes.add( mapShapes.get( 1 ) );					
					mtk.addShapes( theShapes );
					mtk.selectShapes( theShapes );
					
					break;					
						
				case UndoActions.INSERT_POLYGON_POINT:		    
				case UndoActions.DELETE_POLYGON_POINT:
				case UndoActions.MOVE_POLYGON_POINT:
					List<PolygonShapeUndoInfo> undoInfos =(List<PolygonShapeUndoInfo>)redoAction.getObject();
					for( PolygonShapeUndoInfo undoInfo : undoInfos ) {
						PolygonShapeUndoInfo undoInfoSav =new PolygonShapeUndoInfoImpl();
						undoInfoSav.cloneInfoFromPolygonShape( undoInfo.getPolygonShape() );

						undoInfo.getPolygonShape().setGeneralPath( undoInfo.getGeneralPath() );
						undoInfo.getPolygonShape().setDisputedBorders( undoInfo.getDisputedBorders() );
						
						undoInfo.setGeneralPath( undoInfoSav.getGeneralPath() );
						undoInfo.setDisputedBorders( undoInfoSav.getDisputedBorders() );
					}
				    
				    break;	
				    
				case UndoActions.DISPUTED_BORDER:
					mtk.addDisputedBorder( (Point2D)redoAction.getObject() );
					
					break;	
					
				case UndoActions.FILL:
					shape =(MapShape)redoAction.getObject();
					Color color =shape.getFillColor();
					shape.setFillColor( redoAction.getColor() );
					redoAction.setColor( color );
					
					break;		
					
				case UndoActions.ADD_CD_POINT:
					List row =(List)redoAction.getObject();
					mtk.getThematicMap().getData().getRows().add( row );	
					
					break;
					
				case UndoActions.MOVE_POINT:
					shape =(MapShape)redoAction.getObject();
					shape.updateShape( redoAction.getHandleIdx(), redoAction.getDragPt() );
					
					break;					
			}
		}
		
		return redoAction;
		
	}

}
